package de.rubaff.reqiengi;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.inject.Guice;
import com.google.inject.Injector;

import de.rubaff.reqiengi.database.DatabaseManager;
import de.rubaff.reqiengi.objects.Project;

/**
 * Servlet implementation class ListProjectServerlet
 */
public class ListProjectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListProjectServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
	{
		//Dummy.CreateDummyProject();
		
		
		Injector injector = Guice.createInjector();
		
		// DatabaseManagerinstance erzeugen
		DatabaseManager manager = injector.getInstance(DatabaseManager.class);
		manager.openDatabase(DatabaseManager.DB);
		
		//Neues Dummy Project erstellen ohne "new" und gleich mit Inhalt aber ohne zuweisung.
		//Die Werte stehen in den Konstruktor von User und Requirement drin.
		Project testproject = injector.getInstance(Project.class);
		System.out.println("Username : " + testproject.getUsers().get(0).getName());
		System.out.println("Requirement : " + testproject.getRequirements().get(0).getName());
		System.out.println("Requirement : " + testproject.getRequirements().get(0).getText());
				
		
		List<Project> temp = manager.getAllProjects();			
		List<Project> result = new ArrayList<Project>();
		for(Project pro : temp)
		{			
			result.add(pro);
		}
		
		manager.closeDatabase();
		
		
		
		req.setAttribute("projects",result );
		req.getRequestDispatcher("/start.jsp").forward(req,resp);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
